package com.crack.nga2.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.crack.nga2.R;
import com.crack.nga2.custom.HeadLayoutImpl;
import com.crack.nga2.custom.ILoadingLayout;
import com.crack.nga2.entity.SubForum;
import com.crack.nga2.utils.GlobalConst;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class ForumFragment extends Fragment {


    private View rootView;
    private ViewPager viewPager;
    private HorizontalScrollView scrollView;
    private RadioGroup radioGroup;
    private View header;
    private ILoadingLayout refreshLayout;
    private RadioButton[] buttons = new RadioButton[7];
    private SubForumFragment[] fragments = new SubForumFragment[7];
    private String[] strings = new String[7];
    private Map<String, List<SubForum>> map = new HashMap<>();
    private float button_width;

    public ForumFragment() {
        // Required empty public constructor
    }

    /**
     * 获取数据
     */
    public void setForumMap(Map<String, List<SubForum>> map) {
        this.map = map;
        for (int i = 0; i < strings.length; i++) {
            fragments[i].addForumList(this.map.get(strings[i]));
        }
        stopRefresh();
    }

    public void stopRefresh() {
        refreshLayout.stopPull();
        refreshLayout.stopRefresh();
        header.setVisibility(View.GONE);
    }


    public void addCollect(SubForum forum) {
        fragments[0].addForum(forum);
    }

    public void removeCollect(SubForum forum) {
        fragments[0].removeForum(forum);
    }

    public boolean isCollected(SubForum forum) {
        return fragments[0].contains(forum);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_forum, container, false);
        viewPager = (ViewPager) rootView.findViewById(R.id.viewPager);
        scrollView = (HorizontalScrollView) rootView.findViewById(R.id.scrollView);
        radioGroup = (RadioGroup) rootView.findViewById(R.id.radioGroup);
        header = rootView.findViewById(R.id.refresh_header);
        refreshLayout = new HeadLayoutImpl(getContext(), header);
        init();
        setViews();
        refreshLayout.startRefresh();
        return rootView;
    }

    private void init() {
        button_width = getActivity().getResources().getDimension(R.dimen.radio_button_width);
        strings[0] = getActivity().getString(R.string.tab0);
        strings[1] = getActivity().getString(R.string.tab1);
        strings[2] = getActivity().getString(R.string.tab2);
        strings[3] = getActivity().getString(R.string.tab3);
        strings[4] = getActivity().getString(R.string.tab4);
        strings[5] = getActivity().getString(R.string.tab5);
        strings[6] = getActivity().getString(R.string.tab6);
        buttons[0] = (RadioButton) rootView.findViewById(R.id.radio1);
        buttons[1] = (RadioButton) rootView.findViewById(R.id.radio2);
        buttons[2] = (RadioButton) rootView.findViewById(R.id.radio3);
        buttons[3] = (RadioButton) rootView.findViewById(R.id.radio4);
        buttons[4] = (RadioButton) rootView.findViewById(R.id.radio5);
        buttons[5] = (RadioButton) rootView.findViewById(R.id.radio6);
        buttons[6] = (RadioButton) rootView.findViewById(R.id.radio7);
        for (int i = 0; i < strings.length; i++) {
            fragments[i] = new SubForumFragment();
            //设置角标
            fragments[i].setIndex(i);
        }
        viewPager.setAdapter(new ForumFragmentAdapter(getChildFragmentManager()));
    }

    private void setViews() {

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                buttons[position].setChecked(true);
                //计算scrollView须要平移的距离
                //屏幕宽度
                int screen_width = GlobalConst.SCREEN_WIDTH;
                //偏移量
                float offset = (float) ((position + 0.5) * button_width - screen_width / 2);
                //获取当前位置
                scrollView.scrollTo((int) offset, 0);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < buttons.length; i++) {
                    RadioButton button = buttons[i];
                    if (button.getId() == checkedId) {
                        viewPager.setCurrentItem(i);
                        return;
                    }
                }
            }
        });
    }


    private class ForumFragmentAdapter extends FragmentPagerAdapter {

        public ForumFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments[position];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }
    }

}
